package cn.gdcp.recyclerview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity{
    private RecyclerView rlvStu ;
    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private ArrayList<Student> searchResultList = new ArrayList<>();
    private StuAdapter adapter;
    private Button btnAddStu;

    private EditText edtKeyword;
    private Button btnSearch;
    private boolean isResultList;

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initStuAdapter();
        initStuList();
        initAddBtn();
        initList();
        initSearchView();
    }

    private void initSearchView() {
       edtKeyword = findViewById(R.id.edt_keyword);
       btnSearch = findViewById(R.id.btn_search);
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String keyword = edtKeyword.getText().toString().trim();
                search(studentArrayList,keyword);
                adapter.changeData(searchResultList);
                isResultList = true;
            }
        });
        edtKeyword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String keyword = edtKeyword.getText().toString().trim();
                search(studentArrayList,keyword);
                adapter.changeData(searchResultList);
                isResultList = true;
            }
        });
    }

    private void search(ArrayList<Student> studentArrayList, String keyword) {
        searchResultList.clear();
        for(int i=0;i<studentArrayList.size();i++){
            Student s = studentArrayList.get(i);
            if(s.getName().contains(keyword)){
                searchResultList.add(s);
            }
            else if (String.valueOf(s.getAge()).equals(keyword)){
                searchResultList.add(s);
            }
        }
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        Log.e("MainActivity","requestCode="+requestCode+"resultCode"+resultCode);

        if(data==null){
            return;
        }
        if (resultCode==2001){
        String name = data.getStringExtra("NAME");
        int age = data.getIntExtra("AGE",0);
        int imgId = data.getIntExtra("IMG",R.drawable.dog1);

        Student s = new Student(name,"18006",age,imgId);
        studentArrayList.add(s);
        adapter.notifyDataSetChanged();
        }else if(resultCode == 3001){
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE",0);
            int imgId = data.getIntExtra("IMG",R.drawable.dog1);
            String no = data.getStringExtra("NO");

            for(int i=0;i<studentArrayList.size();i++){
                Student s = studentArrayList.get(i);

                if(s.getNo().equals(no)){
                    s.setName(name);;
                    s.setAge(age);
                    s.setImgId(imgId);
                }
            }
            //adapter.notifyDataSetChanged();
            String keyword = edtKeyword.getText().toString().trim();
            search(studentArrayList,keyword);
            adapter.changeData(searchResultList);
            isResultList = true;
        }
    }
    public void initList(){
       rlvStu = findViewById(R.id.lvr);
       rlvStu.setAdapter(adapter);
       adapter.setItemClick(new IOnitemClick(){
           public void IOnitemClick(int i, View v) {
               Log.e("a","位置："+i);
               Student s =  studentArrayList.get(i);
               Intent intent = new Intent();
               intent.setClass(MainActivity.this,
                       EditActivity.class);
               intent.putExtra("NAME", s.getName());
               intent.putExtra("AGE", s.getAge());
               intent.putExtra("IMGID",s.getImgId());
               intent.putExtra("NO", s.getNo());
               startActivityForResult(intent, 1002);
           }
       });
    }

    /*
    * 初始化添加按钮
    * */
    private void initAddBtn() {
       btnAddStu = findViewById(R.id.btn_addStu);
       btnAddStu.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent = new Intent();
               intent.setClass(MainActivity.this,AddActivity.class);
               startActivityForResult(intent,1001);
           }
       });
    }

    /*
    * 初始化适配器
    * */
    private void initStuAdapter() {
        rlvStu = findViewById(R.id.lvr);
        adapter = new StuAdapter(MainActivity.this,studentArrayList);
        rlvStu.setAdapter(adapter);
        LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
        rlvStu.setLayoutManager(manager);
    }

    /*
    * 初始化学生列表
    * */
    private void initStuList() {
        Student s1 = new Student("A","18001",21,R.drawable.dog1);
        Student s2 = new Student("B","18002",22,R.drawable.dog2);
        Student s3 = new Student("C","18003",23,R.drawable.dog3);
        Student s4 = new Student("D","18004",24,R.drawable.dog4);
        Student s5 = new Student("E","18005",25,R.drawable.dog5);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
    }
    public void onDeleteClick(int position) {
        Toast.makeText(MainActivity.this, "onItemDelete position=" + position, Toast.LENGTH_SHORT).show();
        studentArrayList.remove(position - 1);
        adapter.notifyItemRemoved(position);
    }

}
